﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Upeu.Matricula.Data.Entities;

namespace Upeu.Matricula.Data.Business
{
    public class StudentBusiness
    {
        public static IList<Student> GetListByFilter(string filter)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                return ctx.Students.Where(u => u.Nombre.Contains(filter)).Include(t => t.Tutor).ToList();
            }
        }

        public static Student Create(Student Student)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Student.Tutor = ctx.Tutor.Find(Student.Tutor_Id);
                Student StudentDB = ctx.Students.Add(Student);

                ctx.SaveChanges();
                return StudentDB;
            }
        }
        public static Student Update(Student Student)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Student.Tutor = ctx.Tutor.Find(Student.Tutor_Id);
                ctx.Entry(Student).State = EntityState.Modified;

                ctx.SaveChanges();
                return Student;
            }
        }
        public static Student GetById(Guid id)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Student StudentDB = ctx.Students.Find(id);
                if (StudentDB == null)
                {
                    throw new Exception("Student not found in data base");
                }
                return StudentDB;
            }
        }
        public static void Delete(Guid id)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Student StudentDB = ctx.Students.Find(id);
                if (StudentDB == null)
                {
                    throw new Exception("Student not found in data base");
                }
                ctx.Students.Remove(StudentDB);
                ctx.SaveChanges();

            }
        }
    }
}
